﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using eventr.ImageViewDataService;
using System.ServiceModel;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace eventr.Model
{
    public class ImageDataHelper
    {

        ImageViewDataService.ImageViewCounterClient service;        

        public void SaveImageView(int imageId)
        {
            service.SaveImageViewAsync(imageId);
        }

        public void GetViewData(Action<ObservableCollection<ImageMetaData>> callback)
        {
            service.GetImageViewsAsync(callback);
        }

        public ImageDataHelper ()
        {
            BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.None);

            binding.MaxReceivedMessageSize = 10000000;

            Uri u = new Uri(App.Current.Host.Source, "../Service/ImageViewCounter.svc");

            EndpointAddress endpoint = new EndpointAddress(u);

            service = new ImageViewCounterClient(binding, endpoint);

            service.GetImageViewsCompleted += new EventHandler<GetImageViewsCompletedEventArgs>(service_GetImageViewsCompleted);
        }

        void service_GetImageViewsCompleted(object sender, GetImageViewsCompletedEventArgs e)
        {
            Action<ObservableCollection<ImageMetaData>> t = e.UserState as Action<ObservableCollection<ImageMetaData>>;
            if (t != null)
            {
                t(e.Result);
            }
        }
    }
}
